package study.saas.order.service;

import org.springframework.stereotype.Service;
import study.saas.common.entity.YPageable;
import study.saas.common.repo.Repo;
import study.saas.common.util.CopyClassUtil;
import study.saas.common.util.PageUtils;
import study.saas.common.util.StringUtil;
import study.saas.common.viewmodel.ApiResultV2;
import study.saas.common.viewmodel.PageModel;
import study.saas.order.entity.Commodity;
import study.saas.order.entity.Purchase;
import study.saas.order.repo.CommodityRepo;
import study.saas.order.repo.PurchaseRepo;
import study.saas.order.service.model.PurchaseConditionDTO;
import study.saas.order.service.model.PurchaseCreateDTO;
import study.saas.order.service.model.PurchaseFilterDTO;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author HuBoyu
 */
@Service
public class PurchaseService {
    @Resource
    private PurchaseRepo purchaseRepo;
    private CommodityRepo commodityRepo;

    public PurchaseService(CommodityRepo commodityRepo) {
        this.commodityRepo = commodityRepo;
    }


    public ApiResultV2<Purchase> get(Integer purchaseId) {
        Purchase purchase = purchaseRepo.findFirstByPurchaseId(purchaseId);
        if (purchase != null && purchase.isDelete()) {
            purchase = null;
        }
        return ApiResultV2.ok(purchase);
    }

    public ApiResultV2<PageModel<Purchase>> list(PurchaseFilterDTO filter) {
        YPageable pageable = PageUtils.Ypageable(filter.getPage(), filter.getPageSize());
        StringBuilder sql = new StringBuilder();
        Map<String, Object> param = new HashMap<>();
        sql.append(" select p from Purchase p where p.isDeleted = 0 ");
        if (!StringUtil.isEmpty(filter.getPurchaseNameEq())) {
            sql.append(" and p.purchaseNameEq = :getPurchaseNameEq");
            param.put("getPurchaseNameEq", filter.getPurchaseNameEq());
        }
        if (!StringUtil.isEmpty(filter.getPurchaseNameLike())) {
            sql.append(" and p.purchaseNameEq like :getPurchaseNameLike");
            param.put("getPurchaseNameLike", "%" + filter.getPurchaseNameLike()+ "%");
        }
        sql.append(" order by p.createTime desc,uuid desc ");
        pageable.setCountSql(Repo.toCountJPql(sql.toString()));
        PageModel<Purchase> pageModel = purchaseRepo.queryPage(sql.toString(), param, Purchase.class, pageable);
        return ApiResultV2.ok(pageModel);
    }

    public ApiResultV2<PageModel<Purchase>> conditionList(PurchaseConditionDTO filter){
        YPageable pageable = PageUtils.Ypageable(filter.getPage(),filter.getPageSize());
        StringBuilder sql = new StringBuilder();
        Map<String ,Object> param = new HashMap<>();
        sql.append(" select p from Purchase p where p.isDeleted = 0");
        if(!StringUtil.isEmpty(filter.getPurchaseNameEq())) {
            sql.append(" and p.purchaseNameEq = :getPurchaseNameEq");
            param.put("getPurchaseNameEq", filter.getPurchaseNameEq());

        }
        sql.append(" and p.purchaseId = :getPurchaseId");
        param.put("getPurchaseId", filter.getPurchaseId());
        sql.append(" order by p.createTime desc,uuid desc ");
        pageable.setCountSql(Repo.toCountJPql(sql.toString()));
        PageModel<Purchase> pageModel1 = purchaseRepo.queryPage(sql.toString(), param, Purchase.class, pageable);
        return ApiResultV2.ok(pageModel1);



    }

    public ApiResultV2 purchaseCreate(PurchaseCreateDTO purchaseCreate){
       Purchase purchase  = purchaseRepo.findFirstByPurchaseId(purchaseCreate.getPurchaseId());
       Commodity commodity = commodityRepo.findFirstById(purchaseCreate.getId());
       Purchase newPurchase;
       if (purchase!=null&&!purchase.isDelete()){
           return ApiResultV2.error("订单已存在");
       }else if (commodity == null||commodity.isDelete()) {
           return ApiResultV2.error("该商品不存在");
        }else if (purchase==null){
           if(commodity.getAmount()<purchaseCreate.getAmount()){
               return ApiResultV2.error("库存不足");
           }
           commodity.setAmount(commodity.getAmount()-purchaseCreate.getAmount());
           commodityRepo.save(commodity);
           newPurchase= new Purchase();


       }
       else {
           newPurchase = purchase;
           newPurchase.setIsDeleted(0);
       }
       CopyClassUtil.copyClass(purchaseCreate, newPurchase);
       purchaseRepo.save(newPurchase);
       return ApiResultV2.ok();

    }

    public ApiResultV2 deletePurchase(Integer purchaseId) {
        Purchase purchase = purchaseRepo.findFirstByPurchaseId(purchaseId);
        if (purchase == null || purchase.isDelete()) {
            return ApiResultV2.ok();
        }
        purchase.setIsDeleted(1);
        purchaseRepo.save(purchase);
        return ApiResultV2.ok();
    }


    public ApiResultV2<List<Purchase>> selectAll() {
        List<Purchase> list = purchaseRepo.findAll();
        return ApiResultV2.ok(list);
    }
}

